//
//  ZLZKBaseUtils.m
//  dsBridge
//
//  Created by sxh on 2020/9/16.
//

#import "ZLZKBaseUtils.h"
#import <iflyMSC/iflyMSC.h>
#import <objc/runtime.h>
#import "macro.h"
#import <UserNotifications/UserNotifications.h>

#define IFLY_APPID @"5f47626e"

@interface ZLZKBaseUtils()
<
    UNUserNotificationCenterDelegate
>

@end

@implementation ZLZKBaseUtils

+ (instancetype)sharedInstace {
    static ZLZKBaseUtils *shareInstace = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstace = [[ZLZKBaseUtils alloc] init];
    });
    return shareInstace;
}


- (BOOL)initWithLaunchOptions:(NSDictionary *)launchOptions {

    // iOS10系统以上
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            NSLog(@"注册成功");
            [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                NSLog(@"%@", settings);
            }];
        } else {
            // 用户选择了不允许
            NSLog(@"注册失败");
        }
    }];

    #if TARGET_IPHONE_SIMULATOR  //模拟器
    #elif TARGET_OS_IPHONE //真机
        Class ifly = NSClassFromString(@"IFlySetting");
        NSString *iflyName = NSStringFromClass(ifly);
        if (iflyName.length) {
        
            //Set log level
            [IFlySetting setLogFile:LVL_ALL];

            //Set whether to output log messages in Xcode console
            [IFlySetting showLogcat:YES];

            //Set the local storage path of SDK
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *cachePath = [paths objectAtIndex:0];
            [IFlySetting setLogFilePath:cachePath];
            //初始化讯飞语音识别
            NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@", IFLY_APPID];

            [IFlySpeechUtility createUtility:initString];
        }
    #endif
    
    return YES;
}

# pragma mark -- 获取deviceToken
// 注册远程通知成功后，会调用这个方法，把 deviceToken 返回给我们
- (void)registerForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    if (![deviceToken isKindOfClass:[NSData class]]) {
        
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"deviceToken"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        return;
    }
    const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
    NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                          ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                          ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                          ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
    
    [[NSUserDefaults standardUserDefaults] setObject:hexToken forKey:@"deviceToken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    NSLog(@"deviceToken:%@",hexToken);
}

// 注册远程通知失败后，会调用这个方法
- (void)failToRegisterForRemoteNotificationsWithError:(NSError *)error {
     NSLog(@"注册远程通知失败----%@", error.localizedDescription);
}

@end
